//
//  HisAllCell.m
//  夺宝吧
//
//  Created by xiaocai on 15/8/19.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "HisAllCell.h"
#import "ShowNumView.h"
#import "LockView.h"
#import "PdcDetailVC.h"
@interface HisAllCell ()
{
    __weak IBOutlet UIImageView *img;
    __weak IBOutlet UILabel *titleLab;
    __weak IBOutlet UILabel *allLab;
    __weak IBOutlet UILabel *ygmLab;
    __weak IBOutlet UILabel *leftLab;
    __weak IBOutlet UILabel *cyLab;
    __weak IBOutlet UIButton *buyBtn;
    __weak IBOutlet UIButton *showBtn;
    
    RecordHisModel *_currentModel;
    
    UIProgressView *progressView;
    
    __weak IBOutlet UIButton *is_locked;
}
@end
@implementation HisAllCell

- (void)awakeFromNib {
    // Initialization code
    [self addSubview:[WYPublic getFenGexian:0 :self.frame.size.height-0.5 :WYMWITH :0.5]];
    buyBtn.layer.cornerRadius = 2;
    buyBtn.layer.masksToBounds = YES;
    
    //改变进度条的默认高度
    progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
    // progressView's frame will be small/standard height
    progressView.frame = CGRectMake(CGRectGetMinX(allLab.frame), (CGRectGetMidY(ygmLab.frame)+CGRectGetMaxY(allLab.frame))/2-5, 174*AUTOX, 2);
    progressView.progressTintColor = [UIColor redColor];
    progressView.layer.masksToBounds = YES;
    progressView.layer.cornerRadius = 2;
    
    //改变进度条的默认高度
    CGAffineTransform transform = CGAffineTransformMakeScale(1.0f, 2.8f);
    progressView.transform = transform;
    [self addSubview:progressView];

}

- (void)fillCellWithModel:(RecordHisModel *)model
{
    _currentModel = model;
    [img sd_setImageWithURL:[NSURL URLWithString:model.his_record_imgPath] placeholderImage:[UIImage imageNamed:@"moren"]];
    titleLab.text = [NSString stringWithFormat:@"(第%@期)%@",model.his_record_qishu,model.his_record_title];
    
    cyLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"参与次数:" Middle:model.his_record_gonumber RightStr:@"次" Font:12];
    if(model.isMineFlag){
        [buyBtn setTitle:@"追加" forState:UIControlStateNormal];
        
    }else{
        [buyBtn setTitle:@"跟买" forState:UIControlStateNormal];
//        [buyBtn addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
    }
    allLab.text = [NSString stringWithFormat:@"总需:%@次",model.his_all_need];
    ygmLab.text = [NSString stringWithFormat:@"已购买:%ld次",(unsigned long)([model.his_all_need integerValue] - [model.his_left_need integerValue])];
    leftLab.attributedText = [WYPublic getRedMiddleStrWithLeft:@"剩余:" Middle:model.his_left_need RightStr:@"次" Font:12];
    progressView.progress = ([model.his_all_need integerValue] - [model.his_left_need integerValue])*1.0/[model.his_all_need integerValue];
    
    if ([model.his_statu intValue] == 2||[model.his_statu intValue] ==4||[model.his_statu intValue] ==5) {
        buyBtn.hidden = NO;
//        [buyBtn removeTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
        [buyBtn setTitle:@"待揭晓" forState:UIControlStateNormal];
        [buyBtn addTarget:self action:@selector(goToPdcDetail:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        buyBtn.hidden = NO;
        [buyBtn addTarget:self action:@selector(buy:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    NSString *show_shopping = [[NSUserDefaults standardUserDefaults] valueForKey:@"show_shopping"];
    [buyBtn setHidden:YES];
    if (show_shopping.integerValue == 1) {
        [buyBtn setHidden:NO];
    }
    
    if (_currentModel.is_locked.integerValue == 1) {
        [is_locked setHidden:NO];
        [progressView setProgressTintColor:[UIColor colorWithRed:238/255.0 green:170/255.0 blue:0/2555.0 alpha:1.0]];
    }else{
        [progressView setProgressTintColor:[UIColor redColor]];
        [is_locked setHidden:YES];
    }
}

- (void)goToPdcDetail:(UIButton *)sender{
    //跳往商品详情
    
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = _currentModel.his_record_id;
    
    
    [[WYMainTabBarVC shareMainTaBarVC].navigationController  pushViewController:detailVC animated:YES];
}

- (IBAction)lockTapped:(UIButton *)sender
{
    [WYPublic addHudToWindow];
    NSDictionary *params = @{@"id":_currentModel.his_record_id};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,@"api/ApiGoods/getLockedList"] Parameters:params successBlock:^(NSDictionary *dic) {
        [WYPublic removeHudToWindow];
        NSArray *array = dic[@"data"][@"locked_list"];
        if ([array count] > 0) {
            LockView *lockVIew = [[LockView alloc]initWithArr:dic[@"data"][@"locked_list"] Count:dic[@"data"][@"locked_total"]];
            [WINDOW addSubview:lockVIew];
        }
        else {
            [WYPublic showMessageWithTitle:@"提示:" Msg:@"信息已过期，请重新刷新页面"];
            [WYPublic removeHudToWindow];
        }
        
    } failedBlock:^{
        [WYPublic showMessageWithTitle:@"提示:" Msg:@"信息已过期，请重新刷新页面"];
        [WYPublic removeHudToWindow];
    } needLog:NO];
}

- (IBAction)buy:(id)sender {
    
    WYShopCarModel *car_model = [[WYShopCarModel alloc]init];
    //期数
    car_model.qishu = [WYPublic getUsefullStr:_currentModel.his_record_qishu];
    car_model.car_id = [WYPublic getUsefullStr:_currentModel.his_record_id];
    car_model.car_new_id = [WYPublic getUsefullStr:_currentModel.his_record_sid];
    car_model.imgPath = [WYPublic getUsefullStr:_currentModel.his_record_imgPath];
    car_model.name = [WYPublic getUsefullStr:_currentModel.his_record_title];
    car_model.all_need = [WYPublic getUsefullStr:_currentModel.his_record_moneycount];
    car_model.left_need = [WYPublic getUsefullStr:_currentModel.his_record_gonumber];
    //加入购物车
    if(car_model != nil){
        if ([[WYDataBase sharedDataBase]insertPdcToCarWithModel:car_model]) {
            [[WYMainTabBarVC shareMainTaBarVC] changeSelectedWithIndex:2];
            [[WYMainTabBarVC shareMainTaBarVC]updateCountToCar];
            [[WYMainTabBarVC shareMainTaBarVC].navigationController popToRootViewControllerAnimated:YES];
        }
        
    }

}
- (IBAction)show:(id)sender {
    ShowNumView *his = [[ShowNumView alloc]initWithArr:_currentModel.his_record_nums AndQiShu:_currentModel.his_record_qishu AndTitle:_currentModel.his_record_title];
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:his];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
